﻿#region Using

using System.Collections.Generic;

using GameLogic.External;

#endregion

namespace CommunicationLibrary.TransportData
{
    public class InstanceManager
    {
        private static InstanceManager instance;

        private readonly Dictionary<int, Building> buildings = new Dictionary<int, Building>();

        private readonly Dictionary<int, Player> players = new Dictionary<int, Player>();

        private readonly Dictionary<int, Route> routes = new Dictionary<int, Route>();

        private readonly Dictionary<int, Tile> tiles = new Dictionary<int, Tile>();

        private readonly Dictionary<int, Unit> units = new Dictionary<int, Unit>();

        private readonly Dictionary<int, Upgrade> upgrades = new Dictionary<int, Upgrade>();

        private InstanceManager()
        {
        }

        public static InstanceManager Instance
        {
            get
            {
                return instance ?? (instance = new InstanceManager());
            }
        }

        public Building GetOrAddBuilding(IBuilding argBuilding)
        {
            Building building;

            if (!this.buildings.TryGetValue(argBuilding.Identifier, out building))
            {
                building = new Building(argBuilding);
                this.buildings.Add(argBuilding.Identifier, building);
            }

            return building;
        }

        public Tile GetOrAddTile(ITile argTile)
        {
            Tile tile;

            if (!this.tiles.TryGetValue(argTile.Identifier, out tile))
            {
                tile = new Tile(argTile);
                this.tiles.Add(argTile.Identifier, tile);
            }

            return tile;
        }

        public Player GetOrAddPlayer(IPlayer argPlayer)
        {
            Player player;

            if (!this.players.TryGetValue(argPlayer.Identifier, out player))
            {
                player = new Player(argPlayer);
                this.players.Add(argPlayer.Identifier, player);
            }

            return player;
        }

        public Route GetOrAddRoute(IRoute argRoute)
        {
            Route route;

            if (!this.routes.TryGetValue(argRoute.Identifier, out route))
            {
                route = new Route(argRoute);
                this.routes.Add(argRoute.Identifier, route);
            }

            return route;
        }

        public Unit GetOrAddUnit(IUnit argUnit)
        {
            Unit unit;

            if (!this.units.TryGetValue(argUnit.Identifier, out unit))
            {
                unit = new Unit(argUnit);
                this.units.Add(argUnit.Identifier, unit);
            }

            return unit;
        }

        public Upgrade GetOrAddUpgrade(IUpgrade argUpgrade)
        {
            Upgrade upgrade;

            if (!this.upgrades.TryGetValue(argUpgrade.Identifier, out upgrade))
            {
                upgrade = new Upgrade();
                this.upgrades.Add(argUpgrade.Identifier, upgrade);
            }

            return upgrade;
        }

        public void Clear()
        {
            this.buildings.Clear();

            this.tiles.Clear();

            this.players.Clear();

            this.routes.Clear();

            this.units.Clear();

            this.upgrades.Clear();
        }
    }
}